package dnl.proj.rami

import org.apache.commons.lang.ArrayUtils
import org.apache.commons.lang.StringUtils

/**
 *
 * @author Daniel Orr
 */

String[] lines = StringUtils.split(s, "\n\r");
String[][] outLines = new String[lines.length];// result lines
for (int i = 0; i < lines.length; i++) {
    String line = lines[i];
    String[] words = StringUtils.split(s, " ");
    ArrayUtils.reverse(words);
    outLines[i] = words;
}

for (int i = 0; i < outLines.length; i++) {
    String[] line = outLines[i];
    if (i > 0) {
        String[] nextLine = outLines[i+1];
        String swap = line[line.length - 1];
        line[line.length - 1] = nextLine[0];
        nextLine[0] = swap;
    }
}

StringBuilder sb = new StringBuilder();
for (int i = 0; i < outLines.length; i++) {
    if (i > 0) {
        sb.append("\n");
    }
    String[] line = outLines[i];
    for (int j = 0; j < line.length ; j++) {
        if(j > 0){
            sb.append(" ");
        }
        sb.append(line[j]);
    }
}

return sb.toString();